package com.aistar.service.impl;

import com.aistar.mapper.FavoritesMapper;
import com.aistar.mapper.ProductMapper;
import com.aistar.pojo.Favorites;
import com.aistar.pojo.FavoritesExample;
import com.aistar.pojo.Product;
import com.aistar.pojo.pojoOV.ProductFavoritsOV;
import com.aistar.service.FavoritsService;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FavoritsServiceImpl implements FavoritsService {

    @Autowired
    ProductMapper productMapper;
    @Autowired
    FavoritesMapper favoritesMapper;

    @Override
    public ServerResponse getAllFavoritsByPage(Integer cusId, Integer pageNum) {

//        List<ProductFavoritsOV> productFavoritsOVList = new ArrayList<>();
//        //        1.先根据用户id找到所有收藏 ，分页
//        FavoritesExample favoritesExample = new FavoritesExample();
//        favoritesExample.createCriteria().andCustomerIdEqualTo(cusId);
//        List<Favorites> favorites = favoritesMapper.selectByExample(favoritesExample);
//        //        2.循环封装ProductFavoritsOv
//        favorites.forEach(favorites1 -> {
//
//            Product product = productMapper.selectByPrimaryKey(favorites1.getCustomerId());
//            ProductFavoritsOV productFavoritsOV = new ProductFavoritsOV(product,favorites1);
//            productFavoritsOVList.add(productFavoritsOV);
//        });
//        //        3.封装pageInfo
//        Page<Favorites> favoritesPage = PageHelper.startPage(pageNum,3);
//        PageInfo pageInfo = favoritesPage.toPageInfo();
//        pageInfo.setList(productFavoritsOVList);

        return null;
    }

    @Override
    public ServerResponse getAllFavorits(Integer cusId) {

        List<ProductFavoritsOV> productFavoritsOVList = new ArrayList<>();
        //        1.先根据用户id找到所有收藏 ，分页
        FavoritesExample favoritesExample = new FavoritesExample();
        favoritesExample.createCriteria().andCustomerIdEqualTo(cusId);
        favoritesExample.createCriteria().andStatusEqualTo("1");
        List<Favorites> favorites = favoritesMapper.selectByExample(favoritesExample);
        //        2.循环封装ProductFavoritsOv
        favorites.forEach(favorites1 -> {

            Product product = productMapper.selectByPrimaryKey(favorites1.getProductId());
            ProductFavoritsOV productFavoritsOV = new ProductFavoritsOV(product,favorites1);
            productFavoritsOVList.add(productFavoritsOV);
        });

        if(favorites != null && favorites.size()>0)
            return ServerResponse.getDataSuccess(productFavoritsOVList);
        else
            return ServerResponse.getDataFailed(productFavoritsOVList);
//        //        3.封装pageInfo
//        Page<Favorites> favoritesPage = PageHelper.startPage(1,3);
//        PageInfo pageInfo = favoritesPage.toPageInfo();
//        pageInfo.setList(productFavoritsOVList);
    }
}
